#include "friend.h"
#include "ui_friend.h"
#include"tcpclient.h"
#include<QInputDialog>
#include<QMessageBox>
#include<QDebug>
#include "groupchat.h"
#include"tipwidget.h"
Friend::Friend(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Friend)
{
    ui->setupUi(this);
    init();
}

Friend::~Friend()
{

    qDeleteAll(m_PrivateChatList);
    m_PrivateChatList.clear();
    delete ui;
}

void Friend::init()
{
    //好友列表
    m_friendListWidget=new QListWidget;
    QString styleSheet = R"(
        QListWidget {
            background-color:rgba(243, 253, 255,220);
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 5px;
        }
    )";
    m_friendListWidget->setStyleSheet(styleSheet);
    m_friendListWidget->setIconSize(QSize(35, 35));


    QHBoxLayout *integralLayout = new QHBoxLayout; // 整体水平布局
    integralLayout -> addWidget(m_friendListWidget);
    setLayout(integralLayout);
}


//更新好友列表
void Friend::updateFriendList(const QMap<QString,QString> &friendList)
{
    // 清除好友列表原有数据
    m_friendListWidget -> clear();
    // 设置单选模式
    m_friendListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    // 清除默认选中状态
    m_friendListWidget->clearSelection();

    QListWidgetItem *pItem = NULL;
    for (QMap<QString, QString>::const_iterator it = friendList.constBegin(); it != friendList.constEnd(); it++) {
        pItem = new QListWidgetItem;
        QString online;
        if(it.value()=="1"){
            online="在线";
            pItem->setIcon(QIcon(QPixmap(":/img/head.png")));
        }else{
            online="离线";
            pItem->setIcon(QIcon(QPixmap(":/img/greyhead.png")));
        }
        pItem->setText(QString("%1\t%2").arg(it.key())
                           .arg(online));
        QFont font = pItem->font();
        font.setPointSize(15);
        pItem->setFont(font);
        m_friendListWidget -> addItem(pItem);
    }
}

QString Friend::searchName()
{
    return m_searchName;
}


//刷新好友列表
void Friend::flushFriendList()
{
    TcpClient::getInstance().flushFriendList();
}


//查找用户
void Friend::searchUsrs()
{
    if(!m_usrDlg){

        m_usrDlg=new ViewUsrsDialog;
        connect(m_usrDlg, &QObject::destroyed, this, [=]() {
            m_usrDlg = nullptr;
        });
        m_usrDlg->show();

        connect(m_usrDlg,&ViewUsrsDialog::searchName,this,[=](const QString& name){
            TcpClient::getInstance().searchUsr(name);
        });

        connect(m_usrDlg,&ViewUsrsDialog::addUsr,this,[=](const QString& name){
            TcpClient::getInstance().addUsr(name);
        });

        connect(m_usrDlg,&ViewUsrsDialog::deleteUsr,this,[=](const QString& name){
            TcpClient::getInstance().deleteFriend(name);
        });

        connect(this,&Friend::sendSearchData,m_usrDlg,&ViewUsrsDialog::getSearchData);
        connect(this,&Friend::flushSignal,m_usrDlg,&ViewUsrsDialog::getFlushSignal);
    }
}

//删除好友
void Friend::deleteFriend()
{
    if(m_friendListWidget -> currentItem()==NULL) // 如果没有选中好友
    {
        TipWidget::getInstance()->showTip("请选择要删除的好友！", TipWidget::Warning, 1000, this);
        return;
    }

    QString friendName=m_friendListWidget -> currentItem()->text();
    friendName=friendName.split("\t").at(0);
    TcpClient::getInstance().deleteFriend(friendName);
}

//主动私聊好友
void Friend::sendPrivateChat()
{
    if(m_friendListWidget -> currentItem()==NULL) // 如果没有选中好友
    {
        TipWidget::getInstance()->showTip("请选择要私聊的好友！", TipWidget::Warning, 1000, this);
        return;
    }
    QString chatName=m_friendListWidget -> currentItem()->text();
    chatName=chatName.split("\t").at(0);            //获取chatName

    PrivateChat *tempChat=searchPrivateChat(chatName);
    if(tempChat==NULL){
        tempChat = new PrivateChat;     //记得delete
        tempChat -> setChatName(chatName);
        m_PrivateChatList.append(tempChat); // 添加入该客户端私聊List
    }
    if(tempChat->isHidden()){
        tempChat->show();
    }
}
//被动私聊好友
void Friend::recvPrivateChat(const QString &chatName, const QString &msg)
{
    PrivateChat *tempChat=searchPrivateChat(chatName);

    if(tempChat==NULL){
        tempChat = new PrivateChat;
        tempChat -> setChatName(chatName);
        m_PrivateChatList.append(tempChat);
    }
    if(tempChat->isHidden()){
        tempChat->show();
    }
    tempChat->updateMsg(msg);
}

//查找聊天窗口
PrivateChat *Friend::searchPrivateChat(const QString &chatName)
{
    for (PrivateChat *ptr:m_PrivateChatList)
    {
        if(ptr->chatName() == chatName)
        {
            return ptr;
        }
    }
    return NULL;
}


//消息广播
void Friend::mesBroadCast()
{
    if(groupChat::getInstance()->isHidden()){
        groupChat::getInstance()->show();
    }
}

void Friend::getFriendSearchData(const QList<UserInfo> &usrList)
{
    emit sendSearchData(usrList);
}

void Friend::flushSearchPage()
{
    emit flushSignal();
}


//校验用户名
bool Friend::validateString(const QString &input) {
    QRegularExpression regex("^[A-Za-z0-9_]{1,31}$");
    QRegularExpressionMatch match = regex.match(input);
    if (!match.hasMatch()) {
        return false;
    }
    return true;
}




